<?php
// +----------------------------------------------------------------------
// | Author: weifashi <605403358@qq.com>
// +----------------------------------------------------------------------
namespace soft;
use think\Request;
use base\other\Strings;
use app\common\model\UserVip;

class Methods {
	
	//替换
	public function replaceExp($str,$attr=null){
		$userInfo =  request()->userInfo;
        preg_match_all( '/(?<={{)[^}}]+/',$str,$match);
        foreach( $match[0] as $val ){
            $v = "{{{$val}}}";
            switch ($val){
            	//new-------
            	case "base:shop_id"://店铺id
			  		$v = $userInfo['shop_id'];
			  		break; 
			  	case "base:当前店铺"://店铺id
			  		$shop_id = $userInfo['shop_id'];
			  		$v = $shop_id ? "shop_id=$shop_id" : '10=10';
			  		break; 	
			  	case "base:user_level"://用户等级
			  		$v = UserVip::where('uid',$userInfo['uid'])->value('level') ?: 0;
			  		break;  
			  	//系统--------
                case "base:uuid"://guid
                    $v = Strings::uuid();
                    break;  
                case "base:date"://时间
			  		$v = date( self::getFormat($attr['format']) );
			  		break;      
                case "base:timeStamp"://guid
                    $v = strtotime('now');
                    break;  
                case "base:本人": //当前用户
                    $v = $userInfo['user_name'];
                    break;
                case "base:本人id": //当前用户id
                    $v = $userInfo['user_id'];
                    break;
                case "base:本人guid": //当前用户id
                    $v = $userInfo['user_guid'];
                    break;  
                case "base:orderNo": //订单号
                    $v = Strings::createOrderNo();
                    break;      
                case "base:str6": //密码
                    $v = Strings::getRandomStr(6);
                    break;      
                case "base:src": //密码
                    $v = Strings::getRandomStr();
                    break;             
                case strstr($val,"url:") ? true : false: //字段
                    $v = self::getParam( str_replace("url:","",$val) );
                    break;
            }
            $str = preg_replace("{{{".$val."}}}", $v ,$str);
        }
        return  $str;
	}
	
	/**
	* 获取xml
	* @param  array  $path  路由
	*/
	public function getXmlFile($path=''){
		if (! function_exists('base_path')) {
		    function base_path($path = ''){
		        return env('root_path').($path ? ltrim($path, DIRECTORY_SEPARATOR) : 
		        ltrim($path, DIRECTORY_SEPARATOR));
		    }
		}
		
		$view_path = config()['template']['view_path'];
		//当前访问路径
		if(request()->isAjax()  || request()->path()== 'admin/core/grid/exportExcel'){
			$referer = urldecode(request()->header()['referer']);
			$url_path =  str_replace(str_replace("\\","/",APP_PATH),'',$referer);
		}else{
			$url_path =  request()->path();
		}
		
		$url_path = $url_path && $url_path !='/' ? $url_path : '/modules/index/index';
	    
		//空路由 默认取当前页面路径的config.xml
		if( empty($path) || !$path ){
			$url='';
			foreach(explode("/",$url_path) as $key => $value){
				if($key != count(explode("/",$url_path))-1){
					$url.='/'.$value;
				}
			}
			$path = $url.'/config.xml';
		}
	    
		//如果有public则替换
		if( strstr($path,"__ROOT__") ){
			$path = base_path().'/'.str_replace("/__root__/","", strtolower($path) );
			$path = str_replace("__root__/","", $path);
			$path = str_replace("__ROOT__/","",$path);
		}else if( strstr($path,"public") || strstr($path,"__PUBLIC__")){
			$path = base_path().'/'.str_replace("/__public__/","public/", strtolower($path) ) ;
			$path =str_replace("__public__/","public/",$path) ;
			$path =str_replace("__PUBLIC__/","public/", $path) ;
		} else if( $path && !strstr(strtolower($path), strtolower($view_path) ) ){
			$path = $view_path.$path;
		}
	    
	    //去除多余路由
		$path = str_replace("index.php","",$path );
		$path = str_replace("///","/", $path );
	    
		//返回
		$xml = simplexml_load_file($path);
		if(!$xml	){
			throw new \think\Exception('找不到xml文件，或xml文件格式错误！'.$path);
		}else{
			return $xml;
		}
	}
	
	
	/**
    * 调用Apl
    * @access private
    * @param string $url 请求路由   			
    * @return  File
    */
	public function callApi($url) {
		$url= trim($url);
		$params=array();
		$params['is_action_call']=true;
		$index = strpos($url,"?");
		if( !empty($index) ){
			$param = substr($url,$index+1,1000);
			$dataUrlParams = explode("&",$param);
			foreach($dataUrlParams as $paramValue){
				$params[explode("=",$paramValue)[0]] = explode("=",$paramValue)[1];
			}
			$url = substr($url,0,$index);
		}
		if(substr($url,0,1) == "/"){
			$url  = substr($url,1,1000);
		}
		$url = str_replace("Admin","admin",$url);
		return  action($url,$params);
	}
	
	/**
    * 获取参数
    */
    public function getParam($field){
       	$v = request()->param("$field")  or "";
		$v= $v ? urldecode($v) : "";
		return $v;
    }
    
    /**
    * 获取日期格式
    * @access private
    * @param string $format  yyyy-MM-dd HH:ii:ss  
    * @return string
    */
	public function getFormat($format){
		$_format='Y-m-d';
		if(!strpos($format,"d") && $format !="")
			$_format='Y-m';
		if(!strpos($format,"M") && $format !="")
			$_format='Y';
		if(strpos($format,"H"))
			$_format='Y-m-d H';
		if(strpos($format,"mm"))
			$_format='Y-m-d H:i';
		if(strpos($format,"s"))
			$_format='Y-m-d H:i:s';
		if( $format == 'HH' || $format == 'HH:mm' || $format == 'HH:mm:ss')
			$_format='H:i:s';
		return $_format;
	}

}




